
import BaseAudioManager from "./BaseAudioManager";
import ResLoader from "../res/ResLoader";
import AudioManager from "./AudioManager";
import GlobalEvent from "../event/GlobalEvent";
import { RewardADState } from "../sdk/base/BaseRewardAd";
import SDKManager from "../sdk/SDKManager";
export default class MusicManager extends BaseAudioManager {


    constructor(loader:ResLoader){
        super(loader);  
        this.audioManager.addEventListener(AudioManager.MUSIC_CLOSE,this.musicClose,this)
        this.audioManager.addEventListener(AudioManager.MUSIC_OPEN,this.musicOpen,this)
        GlobalEvent.instance().addEventListener(GlobalEvent.EVENT_HIDE,this.eventHide,this)
        GlobalEvent.instance().addEventListener(GlobalEvent.EVENT_SHOW,this.eventShow,this) 
        GlobalEvent.instance().addEventListener(GlobalEvent.CHANGE_AD_STATE,this.changeAdState,this)       
    }

    changeAdState(state: RewardADState){
        if(state == RewardADState.close){
            this.eventShow();
        }else{
            this.eventHide();
        }
    }
    eventHide(){
        cc.log(' eventHide this.getMusicFlag()',this.getMusicFlag())
        if(this.getMusicFlag() && this.openName){
            this.pause();
        }
    }

    eventShow(){
        let state =  RewardADState.close // 如果广告没有关闭，回来也不应该有声音
        if(SDKManager.getChannel()){
            state = SDKManager.getChannel().getRewardAdState();
        }
        cc.log(' eventShow this.getMusicFlag()',this.getMusicFlag())
        if(this.getMusicFlag() && state == RewardADState.close){
            if(this.openName){
                this.play(this.openName);
            }    
           
            // this.resume();
        }else{
            cc.audioEngine.stopMusic();
        }
    }

    musicOpen(){
        if(this.openName){
            this.play(this.openName)
        }else{
            this.resume();
        }
    }

    musicClose(){
        this.pause();
    }

    getMusicFlag(){
        return this.audioManager.getMusicFlag();
    }
    

    tempName:string;
    openName:string 
    play(name:string,isLoop:boolean = true){
        this.openName = name;
        if(this.audioManager.getMusicVolume() == 0){
            return ;
        }       
        if(this.tempName){
            this.stop(this.tempName);
            this.tempName = null;
        } 
        super.play(name,isLoop);
    }  
    
    open(name:string){
        this.audioManager.setMusicFlag(true);
        this.play(name);
    }

    close(){
        this.audioManager.setMusicFlag(false);
        if(this.tempName){
            this.stop(this.tempName);
            this.tempName = null;
        }
        
    }

    add(name,res,isLoop){
        this.tempName = name;
        // this.openName = null;
        let audioId = cc.audioEngine.playMusic(res, isLoop);
        BaseAudioManager.sourcePool.set(name,audioId); 
        if(!isLoop){
            cc.audioEngine.setFinishCallback(audioId, this.stop.bind(this, name));    
        }
    }

    pause(){
        if(!this.isPlaying()){
            return;
        }

        cc.audioEngine.pauseMusic();     
    
       
    }

    resume(){
        if(this.isPlaying()){
            cc.log(' is already playing')
            return;
        }        

        cc.audioEngine.resumeMusic();
    
        
    }

    stop(name:string){
       if(!this.isPlaying()){
           return;
       }
       cc.audioEngine.stopMusic();
       if(name){
            let audioId = BaseAudioManager.sourcePool.get(name);
            if(audioId){
                BaseAudioManager.sourcePool.delete(name);
            }
       }
       
    }

    setVolume(count:number){
        cc.audioEngine.setMusicVolume(count);
        this.audioManager.setMusicVolume(count);
    }

    isPlaying(){
        return cc.audioEngine.isMusicPlaying();
    }

}
